// pages/lesson/lesson.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lesson: [],
    hiddendelete: true,
    tips: "长按课程可以删除哦😘",
    lessonid: '',
    addhidden: true,
    lessonName: '',
    classNumber: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.showlesson();
  },
  showlesson: function (e) {
    var that = this;
    var userid = wx.getStorageSync('openid'); //用户id
    wx.request({
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      url: app.globalData.ServerUrl + 'load_lesson.php',
      data: {
        userid: userid
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
          lesson: res.data
        })
      },
      fail: function (res) {
        wx.showToast({
          title: '获取信息失败',
          icon: 'none'
        })
      },
      complete: function (res) {
        if (res.data == '') {
          wx.showToast({
            title: '您还没有创建课程哟！',
            icon: 'none'
          })
        }
      }
    })
  },

  chooselesson: function (e) {
    this.setData({
      lessonid: e.currentTarget.dataset.id,
      lessonName: e.currentTarget.dataset.lesson
    })
    console.log('课程号' + this.data.lessonid)
    console.log('课程名' + this.data.lessonName)
    // this.goSign()
    this.goSignWay()
  },

  goSignWay: function (e) {
    wx.navigateTo({
      url: '../signWay/signWay?lessonid=' + this.data.lessonid,
    })
  },


  deletelesson: function (e) {
    var that = this;
    wx.showModal({
      title: '📢提示',
      content: '确认删除这门课和所有考勤记录？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.ServerUrl + 'delete_lesson.php',
            data: {
              lessonid: e.currentTarget.dataset.id
            },
            method: 'POST',
            header: {
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              console.log(res.data)
              if (res.data) {
                that.showlesson();
                wx.showToast({
                  title: '删除成功！',
                  icon: 'none'
                })
              } else {
                wx.showToast({
                  title: '删除失败！',
                  icon: 'none'
                })
              }
            },
            fail: function (res) {},
            complete: function (res) {},
          })
        }
      }
    })
  },

  goSign: function (e) {
    var that = this;
    console.log(that.data.lessonid)
    wx.request({
      url: app.globalData.ServerUrl + 'goSign.php',
      data: {
        id: that.data.lessonid
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data == null) {
          wx.navigateTo({
            url: '../signByWord/signByWord?lessonid=' + that.data.lessonid + '&lessonName=' + that.data.lessonName + ''
          })
        } else {
          if (res.data.state === 'false') {
            wx.navigateTo({
              url: '../signByWord/signByWord?lessonid=' + that.data.lessonid + '&lessonName=' + that.data.lessonName + ''
            })
          } else {
            wx.navigateTo({
              url: '../signList/signList?lessonid=' + that.data.lessonid + ''
            })
          }
        }
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },



  //获取输入内容
  setLessonName: function (e) {
    this.setData({
      lessonName: e.detail.value
    })
  },
  setClassNumber: function (e) {
    this.setData({
      classNumber: e.detail.value
    })
  },

  addNewLesson: function (e) {
    this.setData({
      addhidden: !this.data.addhidden
    })
  },

  //取消按钮
  cancel: function () {
    this.setData({
      addhidden: true,
    });
  },
  //确认
  confirm: function (e) {
    var that = this;
    this.setData({
      addhidden: true,
    })
    if ((that.data.lessonName && that.data.classNumber) != '') {
      this.charu()
      this.showlesson();
    } else {
      wx.showToast({
        title: '输入为空(+_+)?',
        icon: 'none'
      })
    }
  },

  charu: function () {
    var that = this;
    var userid = wx.getStorageSync('openid');
    console.log(userid)
    wx.request({
      url: app.globalData.ServerUrl + 'addNewLesson.php',
      data: {
        userid: userid,
        lessonName: that.data.lessonName,
        classNumber: that.data.classNumber,
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data == "lesson_add_success") {
          wx.showToast({
            title: '添加成功！',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: '失败请重试！😱',
            icon: 'none'
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})